package org.acegisecurity.util.expressioninterpreter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * Class representing the Not operator. The not operator is a unary operator so it is applied
 * to one value from the value {@link Stack}.
 * </p>
 *
 * @author Jamie Craane
 * @author Ivo Woltring
 */
public class NotOperator extends AbstractOperator {
    private static final Log log = LogFactory.getLog(NotOperator.class);
    public static final String NOT_OPERATOR = "NOT";

    /**
     * {@inheritDoc}
     */
    public boolean apply(String[] roles, Stack valueStack) {
        final String role_1 = (String) valueStack.pop();
        boolean r1 = hasRole(roles, role_1);
        boolean accessGranted = !r1;
        super.pushResultToValueStack(accessGranted, valueStack);
        log.debug("Evaluated NOT [" + role_1 + "] to [" + accessGranted + "]");
        return accessGranted;
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return NOT_OPERATOR;
    }

    /**
     * {@inheritDoc}
     */
    public boolean hasGreaterPrecedenceThan(Operator thisOp) {
        return thisOp instanceof AndOperator || thisOp instanceof OrOperator;

    }
}
